<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>向数组中追加 K 个整数</title>
</head>

<body>
  <script>
    var minimalKSum = function (nums, k) {
      let blocksum = 0
      nums = Array.from(new Set(nums.sort(function (a, b) { return a - b })))
      for (let i = 0; i < nums.length; i++) {
        if (nums[i] <= k) {
          blocksum += nums[i]
          k++
        }
        else { break }

      }
      return ((1 + k) * k) / 2 - blocksum
    }
    console.log(minimalKSum([1, 4, 25, 10, 25], 2))
  </script>
</body>

</html>